//
//  XXViewController.m
//  XX
//
//  Created by lly on 15/5/25.
//  Copyright (c) 2015年 lly. All rights reserved.
//

#import "XXViewController.h"
#import "AppMacro.h"
#import "XXDrawerViewController.h"
#import "UIViewExt.h"
#import "XXLoginViewController.h"
@interface XXViewController ()

@end
#define unLoginView_TAG 111
@implementation XXViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpForDismissKeyboard];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addDrawLeftButton
{
    // 左
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 40, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setBackgroundImage:[UIImage imageNamed:@"sliding_menu_up.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goLeftSideSelector) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}


- (void)goLeftSideSelector
{
    if(self.drawerViewController.openSide == MMDrawerSideLeft)
    {
        [self.drawerViewController closeDrawerAnimated:YES completion:nil];
    }
    else
    {
        [self.drawerViewController openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    }
}
- (void)addCloseNavigationButton
{
    // 左
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 25, 25);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(closeSelf) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)closeSelf
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addUnLoginView
{
    [self removeUnloginView];
    UIView *unloginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    unloginView.tag = unLoginView_TAG;
    unloginView.backgroundColor = [UIColor clearColor];
    unloginView.center = CGPointMake(SCREEN_WIDTH/2, (SCREEN_HEIGHT - 64) / 2);
    [self.view addSubview:unloginView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 30)];
    label.textAlignment = UITextAlignmentCenter;
    label.center = CGPointMake(SCREEN_WIDTH / 2, 10);
    label.textColor = [UIColor blackColor];
    label.text = @"您还未登陆请登陆后查看订单";
    [unloginView addSubview:label];
    
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(20, label.bottom + 20, SCREEN_WIDTH - 40, 45);
    loginButton.backgroundColor = [UIColor colorWithRed:0.271 green:0.647 blue:0.678 alpha:1.000];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"去登陆" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(goLogin) forControlEvents:UIControlEventTouchUpInside];
    loginButton.layer.cornerRadius  = 5.0;
    loginButton.clipsToBounds = YES;
    [unloginView addSubview:loginButton];
}

- (void)removeUnloginView {
    UIView *view = [self.view viewWithTag:unLoginView_TAG];
    [view removeFromSuperview];
}

- (void)goLogin
{
    XXLoginViewController *login = [[XXLoginViewController alloc] init];
    [self.navigationController pushViewController:login animated:YES];
}

#pragma show and hide the keyboard
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
